import { get, post, del } from '@/axios'
import { copyValue } from '@/util'
const moduleUrl = '/pmsClass'
const url = {
	list: moduleUrl + '/list',
	add: moduleUrl + '/add',
	check: moduleUrl + '/check',
	getById: moduleUrl + '/getById',
	update: moduleUrl + '/update',
	del: moduleUrl + '/del',
	start: moduleUrl + '/start',
	end: moduleUrl + '/end'
}
export const activeDict = ['未开班', '进行中', '已结课']
export const createPagination = ({ params, callback }) => {
	return {
	  page: params.pageNo,
	  pageSize: params.pageSize,
	  onChange: (pageNo) => {
	    params.pageNo = pageNo
			callback()
	  },
	  onUpdatePageSize: (pageSize) => {
	    params.pageSize = pageSize
			callback()
		}
	}
}
export const createFormData = async (id) => {
	const formData = {
		name: '',
		createDate: null,
		endDate: null
	}
	if (id) {
		formData.id = id
		const content = await getById(id)
		copyValue(formData, content)
	} 
	return formData
}
export const createRules = (id) => {
	const check = (field, value, callback, id = null) => {
		return new Promise((resolve, reject) => {
			get(url.check, { field, value, id }, content => {
				if (content) {
					resolve()
				} else {
					reject(new Error())
				}
			})
		})
	}
	return {
		name: [
			{ required: true, message: '请输入班级名称', trigger: 'blur' },
			{ max: 255, message: '班级名称最多233个字符', trigger: 'blur' },
			{ validator: (field, value, callback) => check('name', value, callback, id), message: '该班级已经存在，不允许重复创建', trigger: 'blur'}
		],
		createDate: [
			{ required: true, message: '请选择开班日期', trigger: 'blur' }
		],
		endDate: [
			{ required: true, message: '请选择结课日期', trigger: 'blur' }
		]
	}
}
export const getTableData = (params, callback) => {
	get(url.list, params, callback)
}
export const getById = (id) => {
	return new Promise(resolve => {
		get(url.getById, { id }, content => {
			resolve(content)
		})
	})
}
export const addOrUpdate = (id, params, callback, formObj) => {
	const saveUrl = id ? url.update : url.add
	post(saveUrl, params, callback, formObj)
}
export const remove = (id, callback) => {
	del(url.del, { id }, callback)
}
export const start = (id, callback) => {
	post(url.start, { id }, content => {
		callback(content)
	})
}
export const end = (id, callback) => {
	post(url.end, { id }, content => {
		callback(content)
	})
}